﻿#include "netparamform.h"
#include "ui_netparamform.h"
#include <QMessageBox>
#include <QDebug>
#include <QRegExpValidator>

NetParamForm::NetParamForm(QWidget* parent) :
    QWidget(parent),
    ui(new Ui::NetParamForm)
{
    ui->setupUi(this);
    QRegExp reg("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$");
    ui->lineEdit_ip->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_netmask->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_sub_ip->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_gateway->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_dns0->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_dns1->setValidator(new QRegExpValidator(reg));
}

NetParamForm::~NetParamForm()
{
    delete ui;
}
void NetParamForm::onNetworkParam(NetworkParam networkParam)
{
    ui->lineEdit_devtype->setText(networkParam.devtype);
    ui->lineEdit_port->setText(networkParam.port);
    ui->lineEdit_SN->setText(networkParam.sn);
}

void NetParamForm::onGetNetparam(NetParam netparam)
{
    ui->lineEdit_ip->setText(netparam.ip);
    ui->lineEdit_netmask->setText(netparam.mask);
    ui->lineEdit_gateway->setText(netparam.gateway);
    ui->lineEdit_sub_ip->setText(netparam.sub_ip);
    ui->lineEdit_dns0->setText(netparam.dns0);
    ui->lineEdit_dns1->setText(netparam.dns1);

}
void NetParamForm::clear()
{
    ui->lineEdit_devtype->clear();
    ui->lineEdit_ip->clear();
    ui->lineEdit_port->clear();
    ui->lineEdit_netmask->clear();
    ui->lineEdit_gateway->clear();
    ui->lineEdit_devtype->setReadOnly(true);
    ui->lineEdit_ip->setReadOnly(true);
    ui->lineEdit_port->setReadOnly(true);
    ui->lineEdit_netmask->setReadOnly(true);
    ui->lineEdit_gateway->setReadOnly(true);
}

void NetParamForm::on_pushButton_clicked()
{
    //修改
    if(ui->lineEdit_ip->text().isEmpty() || ui->lineEdit_netmask->text().isEmpty()
            || ui->lineEdit_gateway->text().isEmpty() || ui->lineEdit_sub_ip->text().isEmpty()
            || ui->lineEdit_dns0->text().isEmpty()  || ui->lineEdit_dns1->text().isEmpty())
    {
        if(QMessageBox::warning(this, tr("警告"), tr("数据为空"), QMessageBox::Ok, QMessageBox::NoButton))
        {
            return;
        }
    }

    NetParam netparam;
    memset(&netparam, 0, sizeof (netparam));
    memcpy(&netparam.ip, ui->lineEdit_ip->text().toLatin1().data(), sizeof (netparam.ip));
    memcpy(&netparam.mask, ui->lineEdit_netmask->text().toLatin1().data(), sizeof (netparam.mask));
    memcpy(&netparam.gateway, ui->lineEdit_gateway->text().toLatin1().data(), sizeof (netparam.gateway));
    memcpy(&netparam.sub_ip, ui->lineEdit_sub_ip->text().toLatin1().data(), sizeof (netparam.sub_ip));
    memcpy(&netparam.dns0, ui->lineEdit_dns0->text().toLatin1().data(), sizeof (netparam.dns0));
    memcpy(&netparam.dns1, ui->lineEdit_dns1->text().toLatin1().data(), sizeof (netparam.dns1));
    DevScan devScan;
    memset(&devScan, 0, sizeof(devScan));
    devScan.ocmd = DEVICE_SET_NETWORK_CMD ;
    devScan.contentLen = sizeof(netparam);
    memcpy(&devScan.contentstr, &netparam, sizeof(netparam));
    //qDebug() << devScan.contentLen;
    if(ui->lineEdit_adminpsw->text().isEmpty())
    {
        if(QMessageBox::warning(this, tr("警告"), tr("管理员密码为空"), QMessageBox::Ok, QMessageBox::NoButton))
            return;
    }
    else
    {
        if(ui->lineEdit_adminpsw->text() == "hy123456")
            emit sigSendUdpParam(devScan);
        else
        {
            if(QMessageBox::warning(this, tr("警告"), tr("管理员密码错误"), QMessageBox::Ok, QMessageBox::NoButton))
                return;
        }
    }
}

void NetParamForm::on_pushButton_2_clicked()
{
    //重启
    DevScan devScan;
    memset(&devScan, 0, sizeof(devScan));
    devScan.ocmd = DEVICE_SET_REBOOT_CMD ;
    if(ui->lineEdit_adminpsw->text().isEmpty())
    {
        if(QMessageBox::warning(this, tr("警告"), tr("管理员密码为空"), QMessageBox::Ok, QMessageBox::NoButton))
            return;
    }
    else
    {
        if(ui->lineEdit_adminpsw->text() == "hy123456")
            emit sigSendUdpParam(devScan);
        else
        {
            if(QMessageBox::warning(this, tr("警告"), tr("管理员密码错误"), QMessageBox::Ok, QMessageBox::NoButton))
                return;
        }
    }


}
